using System;
using System.Drawing;
using System.Runtime.InteropServices;


namespace Camalon.ColorPicker
{

	
	internal class SafeNativeMethods
	{
		[DllImport("gdi32.dll")]
		private static extern IntPtr CreateDC(string strDriver, string strDevice, string strOutput, IntPtr pData);
		[DllImport("gdi32.dll")]
		[return: MarshalAs(UnmanagedType.Bool)]
		private static extern bool DeleteDC(IntPtr hdc);

		[DllImport("gdi32.dll")]
		private static extern int GetPixel(IntPtr hdc, int x, int y);


		private SafeNativeMethods(){}

		public static Color GetPixelColor( Point screenPoint )
		{

			IntPtr hdcScreen = CreateDC("Display", null, null, IntPtr.Zero);
			int cr = GetPixel(hdcScreen, screenPoint.X, screenPoint.Y);
			DeleteDC(hdcScreen);

			// Convert a Win32 COLORREF to a .NET Color object
			return System.Drawing.ColorTranslator.FromWin32( cr );
			//			return Color.FromArgb((cr & 0x000000FF),
			//				(cr & 0x0000FF00) >> 8,
			//				(cr & 0x00FF0000) >> 16);
		}

		#region COMMENTED SCREEN SHOT FUNCTION
		//		[DllImport("gdi32.dll")]
		//		public static extern bool BitBlt(IntPtr hdcDst, int xDst, int yDst, int cx, int cy,
		//			IntPtr hdcSrc, int xSrc, int ySrc, int ulRop);
		//
		//		public static Bitmap ScreenShot(Rectangle bounds)
		//		{
		//			Graphics grfxScreen = Graphics.FromHwnd(IntPtr.Zero);
		//
		//			// Create a bitmap the size of the screen.
		//			// --Bitmap bm = new Bitmap((int) grfxScreen.VisibleClipBounds.Width, 
		//			// --(int) grfxScreen.VisibleClipBounds.Height, grfxScreen);
		//			// Create a bitmap the size of the specified bounds.
		//			Bitmap bm = new Bitmap(bounds.Width, bounds.Height, grfxScreen);
		//
		//
		//			// Create a Graphics object associated with the bitmap.
		//
		//			Graphics grfxBitmap = Graphics.FromImage(bm);
		//
		//			// Get hdc's associated with the Graphics objects.
		//
		//			IntPtr hdcScreen = grfxScreen.GetHdc();
		//			IntPtr hdcBitmap = grfxBitmap.GetHdc();
		//
		//			// Do the bitblt from the whole screen to the bitmap.
		//			//--BitBlt(hdcBitmap, 0, 0, bm.Width, bm.Height,hdcScreen, 0, 0, 0x00CC0020);
		//			// Do the bitblt from the specific area of the screen to the bitmap.
		//			BitBlt(hdcBitmap, bounds.Left, bounds.Top, bounds.Width, bounds.Height,hdcScreen, 0, 0, 0x00CC0020);
		//
		//			// Release the device contexts.
		//
		//			grfxBitmap.ReleaseHdc(hdcBitmap);
		//			grfxScreen.ReleaseHdc(hdcScreen);
		//
		//			// Manually dispose of the Graphics objects.
		//
		//			grfxBitmap.Dispose();
		//			grfxScreen.Dispose();
		//
		//			return bm;
		//
		//		}
		#endregion COMMENTED SCREEN SHOT FUNCTION
	}
}
